package com.onpositive.funkytable.impl;

import com.onpositive.funkytable.IControlFactory;
import com.onpositive.funkytable.IFunkyComposite;
import com.onpositive.funkytable.ILayoutParams;
import com.onpositive.funkytable.LayoutParams;
import com.onpositive.funkytable.Point;

public class VLayout extends AbstractGroupLayout {

	public VLayout(LabelAndValue[] l, int from, int to) {
		super(l, from, to);
	}

	@Override
	protected Point initLayout(LabelAndValue[] members2) {
		int h = 0;
		int maxW = 0;
		for (LabelAndValue l : members2) {
			int w = 0;
			w += l.measureL.x;
			w += l.measureT.x;
			h = Math.max(h, l.measureL.y);
			h = Math.max(h, l.measureT.y);
			maxW = Math.max(maxW, w);
		}
		return new Point(maxW, h);
	}

	@Override
	public IFunkyComposite create(int width, IControlFactory cf) {
		IFunkyComposite createComposite = cf
				.createComposite(IFunkyComposite.VERTICAL);
		LayoutParams params = new LayoutParams(ILayoutParams.FILL_PARENT,
				ILayoutParams.WRAP_WIDTH);
		LayoutParams params1 = new LayoutParams(ILayoutParams.WRAP_WIDTH,
				ILayoutParams.WRAP_WIDTH);
		LayoutParams params2 = new LayoutParams(ILayoutParams.FILL_PARENT,
				ILayoutParams.WRAP_WIDTH);
		params2.weight = 1;
		for (LabelAndValue l : members) {
			IFunkyComposite createComposite2 = cf
					.createComposite(IFunkyComposite.HORIZONTAL);
			if (l.label != null) {
				createComposite2.add(l.label, params1);
			}
			createComposite2.add(l.value, params2);
			createComposite.add(createComposite2, params);
		}
		return createComposite;
	}

}
